﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class Controls_News_ctrNewsCategory : System.Web.UI.UserControl
{
    
    #region Constants

    private int nCurentLanguage = Support.GetCurrentLanguage() - 1;
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    int CurrentPageIndex = 1;
    int nPageSize = 10;
    int nPageCount = 0;

    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            this.InitData(1);
        }
    }

    #region Repeater rptNewsCat0

    private void InitData(int CurrentPageIndex)
    {

        try
        {
            DataTable dtb = new DataTable();
            NewsCategory objNews = new NewsCategory();
            dtb = objNews.Search();
            rptNewsCat0.DataSource = dtb;
            rptNewsCat0.DataBind();    
          
        }
        catch
        {
            string sURL = Config.ROOT + Pages.FrontEnds.HOME;
            Response.Redirect(sURL);
        }
    }

    protected void rptNewsCat0_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Repeater rptHotNewsTop1 = (Repeater)e.Item.FindControl("rptHotNewsTop1");
            HyperLink lnkNewsTitle = (HyperLink)e.Item.FindControl("lnkNewsTitle");

            int nCat = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "NewsCategoryID"));
            lnkNewsTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Description1")).ToUpper();
            lnkNewsTitle.NavigateUrl = Config.ROOT + Pages.FrontEnds.NEWS_BYCAT + "?" + Config.NEWS_CAT + "=" + nCat; ;

            DataTable dtb = new DataTable();
            News objNews = new News();
            dtb = objNews.Search(1, 0, "", nCat, nDefaultLanguage, nCurentLanguage, EnumeType.ACTIVE, CurrentPageIndex, nPageSize, ref nPageCount);
            rptHotNewsTop1.DataSource = dtb;
            rptHotNewsTop1.DataBind();
        }
    }

    protected void rptNewsCat0_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string sCommand = e.CommandName;
        int nNewsID = ConvertData.ConvertToInt(e.CommandArgument);
        switch (sCommand)
        {
            case "newsimage":
                string sNewsDetail = Config.ROOT + Pages.FrontEnds.NEWS_DETAIL + "?" + Config.NEWS_ID + "=" + nNewsID;
                Response.Redirect(sNewsDetail);
                break;
        }
    }

    #endregion

    #region Repeater rptHotNewsTop1

    protected void rptHotNewsTop1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string sCommand = e.CommandName;
        int nNewsID = ConvertData.ConvertToInt(e.CommandArgument);
        switch (sCommand)
        {
            case "newsimage":
                string sNewsDetail = Config.ROOT + Pages.FrontEnds.NEWS_DETAIL + "?" + Config.NEWS_ID + "=" + nNewsID;
                Response.Redirect(sNewsDetail);
                break;
        }
    }
    protected void rptHotNewsTop1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            ImageButton btnImage = (ImageButton)e.Item.FindControl("btnImage");
            HyperLink lnkTitle = (HyperLink)e.Item.FindControl("lnkTitle");
            Label lnkInitData = (Label)e.Item.FindControl("lnkInitData");
            Repeater rptRelatedNews = (Repeater)e.Item.FindControl("rptRelatedNews");

            int nCatRela = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "NewsCategoryID"));
            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "NewsID"));
            lnkTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsTitle"));
            string sInit = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "InitContent"));
            if (sInit.Length > 150)
            { sInit = ConvertData.TruncateString(sInit, 150) + "..."; }
            lnkInitData.Text = sInit;
            lnkTitle.NavigateUrl = Config.ROOT + Pages.FrontEnds.NEWS_DETAIL + "?" + Config.NEWS_ID + "=" + nID;

            
            string strImages = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Image"));
            if (strImages.Length > 0) { btnImage.ImageUrl = Config.ROOT + Config.IMAGE_NEWS_DEFAULT_UPLOAD + strImages; }
            else { btnImage.ImageUrl = Config.ROOT + Config.IMAGE_NO_IMAGE; }
            btnImage.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsID"));


            

            DataTable dtb = new DataTable();
            News objNews = new News();
            dtb = objNews.Search(5, nID, "", nCatRela, nDefaultLanguage, nCurentLanguage, EnumeType.ACTIVE, CurrentPageIndex, nPageSize, ref nPageCount);
            rptRelatedNews.DataSource = dtb;
            rptRelatedNews.DataBind();
        }
    }

    #endregion

    #region Repeater rptRelatedNews
    protected void rptRelatedNews_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string sCommand = e.CommandName;
        int nNewsID = ConvertData.ConvertToInt(e.CommandArgument);
        switch (sCommand)
        {
            case "newsrelated":
                string sNewsDetail = Config.ROOT + Pages.FrontEnds.NEWS_DETAIL + "?" + Config.NEWS_ID + "=" + nNewsID;
                Response.Redirect(sNewsDetail);
                break;
        }
    }
    protected void rptRelatedNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            LinkButton lbtRelated = (LinkButton)e.Item.FindControl("lbtRelated");
            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "NewsID"));
            lbtRelated.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsTitle"));
            lbtRelated.PostBackUrl = Config.ROOT + Pages.FrontEnds.NEWS_DETAIL + "?" + Config.NEWS_ID + "=" + nID;
            lbtRelated.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsID"));
        }
    }
    #endregion
}
